const format = (date) => {
  const ONE_SECOND = 1000;
  const ONE_MINUTE = 60000;
  const ONE_HOUR = 3600000;
  const ONE_DAY = 86400000;
  const ONE_WEEK = 604800000;
  const ONE_MONTH = 2629746000;
  const ONE_YEAR = 31556952000;
  const ONE_SECOND_AGO = "秒前";
  const ONE_MINUTE_AGO = "分钟前";
  const ONE_HOUR_AGO = "小时前";
  const ONE_DAY_AGO = "天前";
  const ONE_MONTH_AGO = "月前";
  const ONE_YEAR_AGO = "年前";
  let delta = new Date().getTime() - date.getTime()
  if (delta < 1 * ONE_MINUTE) {
    let seconds = ~~(delta / ONE_SECOND);
    return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
  }
  if (delta < 45 * ONE_MINUTE) {
    let minutes = ~~(delta / ONE_MINUTE)
    return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
  }
  if (delta < 24 * ONE_HOUR) {
    let hours = ~~(delta / ONE_HOUR);
    return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
  }
  if (delta < 48 * ONE_HOUR) {
    return "昨天";
  }
  if (delta < 30 * ONE_DAY) {
    let days = ~~(delta / ONE_DAY);
    return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
  }
  if (delta < 12 * 4 * ONE_WEEK) {
    let months = ~~(delta / ONE_MONTH);
    return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
  } else {
    let years = ~~(delta / ONE_YEAR);
    return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
  }
}
//时间转换
const timeFormat = function (timestamp) {
  function add0(m) {
    return m < 10 ? '0' + m : m
  }
  let time = new Date(timestamp);
  let y = time.getFullYear();
  let m = time.getMonth() + 1;
  let d = time.getDate();
  let h = time.getHours();
  let mm = time.getMinutes();
  let s = time.getSeconds();
  return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}
function date_ymdhms(){
  var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      //获取当前时间  
      var n = timestamp * 1000;
      var date = new Date(n);
      //年  
      var Y = date.getFullYear();
      //月  
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
      //日  
      var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
      //时  
      var h = date.getHours();
      //分  
      var m = date.getMinutes();
      //秒  
      var s = date.getSeconds();
      //获取整个时间的格式
      var currentTime = Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s;
  return currentTime;
}


// 时间格式化输出，如03:25:19 86。每10ms都会调用一次
function date_format(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600);
  // 分钟位
  var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
  // 秒位
  var sec = fill_zero_prefix((second - hr * 3600 - min * 60));// equal to => var sec = second % 60;


  return hr + ":" + min + ":" + sec + " ";

  function fill_zero_prefix(num) {
      return num < 10 ? "0" + num : num
  }
}

const formatTime = (time, option) => {
    const date = new Date(time)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const week = date.getDay()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    
    //获取 年月日
    if (option == 'YY-MM-DD') return [year, month, day].map(formatNumber).join('-')
  
    //获取 年月
    if (option == 'YY-MM') return [year, month].map(formatNumber).join('-')
  
    //获取 年
    if (option == 'YY') return [year].map(formatNumber).toString()
  
    //获取 月
    if (option == 'MM') return  [mont].map(formatNumber).toString()
  
    //获取 日
    if (option == 'DD') return [day].map(formatNumber).toString()
  
    //获取 年月日 周一 至 周日
    if (option == 'YY-MM-DD Week')  return [year, month, day].map(formatNumber).join('-') + ' ' + getWeek(week)
  
    //获取 月日 周一 至 周日
    if (option == 'MM-DD Week')  return [month, day].map(formatNumber).join('-') + ' ' + getWeek(week)
  
    //获取 周一 至 周日
    if (option == 'Week')  return getWeek(week)
  
    //获取 时分秒
    if (option == 'hh-mm-ss') return [hour, minute, second].map(formatNumber).join(':')
  
    //获取 时分
    if (option == 'hh-mm') return [hour, minute].map(formatNumber).join(':')
  
    //获取 分秒
    if (option == 'mm-dd') return [minute, second].map(formatNumber).join(':')
  
    //获取 时
    if (option == 'hh')  return [hour].map(formatNumber).toString()
  
    //获取 分
    if (option == 'mm')  return [minute].map(formatNumber).toString()
  
    //获取 秒
    if (option == 'ss') return [second].map(formatNumber).toString()
  
    //默认 时分秒 年月日
    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  }
  
  const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
  }
  
  const getWeek = n => {
    switch(n) {
      case 1:
        return '星期一'
      case 2:
        return '星期二'
      case 3:
        return '星期三'
      case 4:
        return '星期四'
      case 5:
        return '星期五'
      case 6:
        return '星期六'
      case 7:
        return '星期日'
    }
  }
  
// 时间转换 几秒前 之类的...
module.exports = {
  date_format:date_format,
  format: format,
  timeFormat:timeFormat,
  date_ymdhms:date_ymdhms,
  formatTime: formatTime
}